package com.example.demo;


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class Demo1Application extends Application {

    private TextField inputTextField;
    private TextField keyTextField;
    private TextArea outputTextArea;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("加密 App");

        // 创建输入框和按钮
        inputTextField = new TextField();
        inputTextField.setPromptText("输入加密文本");
        keyTextField = new TextField();
        keyTextField.setPromptText("输入加密key");
        Button encryptButton = new Button("加密");

        // 创建文本区域用于显示加密后的内容
        outputTextArea = new TextArea();
        outputTextArea.setEditable(false);
        outputTextArea.setWrapText(true);

        // 设置按钮点击事件
        encryptButton.setOnAction(e -> encrypt());

        // 创建布局
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.add(new Label("加密文本:"), 0, 0);
        gridPane.add(inputTextField, 1, 0);
        gridPane.add(new Label("加密key:"), 0, 1);
        gridPane.add(keyTextField, 1, 1);
        gridPane.add(encryptButton, 0, 2);
        gridPane.add(outputTextArea, 0, 3, 2, 1);

        // 创建场景并显示
        Scene scene = new Scene(gridPane, 400, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    // 加密方法
    private void encrypt() {
        String inputText = inputTextField.getText();
        String key = keyTextField.getText();
        String encryptedText = performEncryption(inputText, key);
        outputTextArea.setText("加密内容: " + encryptedText);
    }

    // 解密方法
    private void decrypt() {
        String inputText = inputTextField.getText();
        String key = keyTextField.getText();
        String encryptedText = performDecryption(inputText, key);
        outputTextArea.setText("解密内容: " + encryptedText);
    }

    // AES加密方法
    private String performEncryption(String input, String key) {
        try {
            StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
            encryptor.setPassword(key);
            return encryptor.encrypt(input);
        } catch (Exception e) {
            e.printStackTrace();
            return "加密过程中出错";
        }
    }

    // AES解密方法
    private String performDecryption(String encryptedText, String key) {
        try {
            StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
            encryptor.setPassword(key);
            return encryptor.decrypt(encryptedText);
        } catch (Exception e) {
            e.printStackTrace();
            return "解密过程中出错";
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
